<?php
/**
 * Created by PhpStorm.
 * User: henryxi-mac
 * Date: 2015/4/15
 * Time: 20:10
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Data extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */

    public function submit(){
        $this->load->model("data_mod");
        $cm = $this->input->post("community");
        $rs = $this->data_mod->cmQuery(array("name" => $cm));
        $accept = empty($rs) ? 0 : 1;
        $res = $this->data_mod->submit($accept);
        $return  = array(
            'code' => 0,
            'msg' => "",
            'data' => array()
        );
        //var_dump($res); exit();


        if (!$res){
            $return['code'] = 4002;
            $return['msg'] ="该手机号已注册";
        }

        if (empty($rs)){
            $return['code'] = 4001;
        }
        $this->output->set_output(json_encode($return));

    }

    public function get_cm_list(){
        $this->load->model("data_mod");
        $cmArray = $this->data_mod->cm_list();
        if (!empty($cmArray)){
            $this->output->set_output(
                json_encode(array(
                    'code' => 0,
                    'msg' => 'OK',
                    "data" => $cmArray
                ))
            );
        }else{
            $this->output->set_output(
                json_encode(array(
                    "code" => 4002,
                    "msg" => "cm list error",
                    "data" => array()
                ))
            );
        }
    }

    public function report(){
        $this->load->helper("ip_helper");
        $newIP = true;
        $pv = $uv = 0;
        $this->load->database();
        $ipinfo = getIP();
        $date = date("Y-m-d",time());
        $querySql = "select * from ip where  ip='".$ipinfo."' and date='{$date}'";
        $return =  $this->db->query($querySql);
        $rs = $return->result();
        if (empty($rs)){
            $sqluv="insert into ip(ip,date) values('{$ipinfo}','{$date}')";
            $this->db->query($sqluv);

        }
        else{
            $newIP = false;
        }

        $pvQuery = "select * from pvuv where date='{$date}'";
        $rs = $this->db->query($pvQuery)->result();

        //var_dump($rs);exit();

        if (empty($rs)){
            $this->db->query("insert into pvuv(pv,uv,sharecount,date) values(1,1,0,'{$date}')");
        }else{
            $pv = $rs[0]->pv+1;
            $uv = $newIP ? $rs[0]->uv +1 : $rs[0]->uv;
            $this->db->query("update pvuv set pv={$pv}, uv={$uv} where date='{$date}'");
        }
    }

    public function share(){
        $this->load->database();
        $date = date("Y-m-d",time());
        $querySql = "select sharecount from pvuv where date='{$date}'";
        $rs = $this->db->query($querySql)->result();
        if (empty($rs) || !$rs){
            $this->report();
        }
        else{
            $sharecount = $rs[0]->sharecount + 1;
            $upsql = "update pvuv set sharecount = {$sharecount} where date='{$date}'";
            $this->db->query($upsql);
        }
        $return = array('code'=> 0, 'msg' => "");
        $this->output->set_output(json_encode($return));
    }
}